﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Bootstrap_Project.Web.Vessel;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.Configuration;
using System.Text;
using Newtonsoft.Json;

namespace Bootstrap_Project.Web.Api_Services
{
    public class VesselController:ApiController
    {

        [System.Web.Http.AcceptVerbs("GET", "POST")]
        [System.Web.Http.HttpGet]
        public ObservableCollection<Vessel.Vessel> GetVessel(string name)
        {
            VesselServiceClient vessClient = new VesselServiceClient("vesselSecureEndpoint");
            string xml = vessClient.PerformOperation("find", string.Format("name:{0}", name));
            Bootstrap_Project.Web.Ports.Reciept r = Serializer.Deserialize<Bootstrap_Project.Web.Ports.Reciept>(xml);
            ObservableCollection<Vessel.Vessel> res = new ObservableCollection<Vessel.Vessel>();
            if (r.Errors.Length == 0)
            {
                res = Serializer.Deserialize<ObservableCollection<Vessel.Vessel>>(r.ResponsePayload);
                for (int i = 0; i < res.Count; i++)
                    if (!res[i].Name.ToLower().StartsWith(name.ToLower()))
                    {
                        res.RemoveAt(i);
                        i--;
                    }
            }
            return res;
        }
    }
}